<?php echo $this->element('admin_search_filter');?>
<?php
echo "<div class='switcher'>";
echo __("Switch to ").$html->link("Listings Settings",array("plugin"=>null,"controller"=>"site_config","action"=>"setting","Listing"));
echo "&nbsp;&nbsp;".$html->link("Listing Categories",array('plugin'=>'categories','controller'=>'categories','action'=>"index"));
echo "</div>";

$Model = cri('Listing');
$fielPropertyObject = $Model->getAttributes($listingData);
$customFields = array();
$customFields = $fielPropertyObject->getFields('group','index');

if(!empty($listingData))
{
	$index->setData($listingData,'Listing'); 
	$showFields = array('id',array('created'=>'Date Added'),'title',array('list_category_id'=>'Category'),array('user_id'=>'Posted By'));
	//merge custome fields.
	if($customFields && isset($showCustomFields) && $showCustomFields == true)
	{
		$showFields = am($showFields,$customFields);
	}
	$sortableFields = array('id','title');
	if(cr("ListingConfig.business_owner"))
	{
		$showFields[] = array('owner_id'=>__("Owner"));
		$sortableFields[] = 'owner_id';
		$index->addColumn('owner_id','setOwnerName');
		$index->setAction(__('Claims'),'/admin/listing_claims/index/:Listing.id');
	}
	$index->showFields($showFields);
	$index->sortablefields($sortableFields);
	
	$index->setAction(__('View'),'/admin/listings/view/:Listing.id');
	$index->setAction(__('Edit'),'/admin/listings/edit/:Listing.id');
	$index->setAction(__('Delete'),'/admin/listings/delete/:Listing.id',$this->helpers,__('Warning! Deleting this listing will delete all associated reviews, comments and images. Are you sure you want to delete this listing ?'));
	$index->setAction(null,null,$this->helpers,false,true,'toggle_featured');
	$index->setAction(null,null,$this->helpers,false,true,'publish_unpublish');
	$index->addButton(__("Delete Selected"),array("action"=>"bulkDelete",'admin'=>true));
	$index->addButton(__("Unpublish Selected"),array("action"=>"bulkUnpublish",'admin'=>true));
	$index->addButton(__("Publish Selected"),array("action"=>"bulkPublish",'admin'=>true));
	
	
	if(cr("DealConfig.enable_deals"))
	{
		$index->setAction(__('Deals'),'/admin/deals/deals/index/:Listing.id');
	}
	if(cr("EventConfig.enable_events"))
	{
		$index->setAction(__('Add Event'),'/admin/events/events/add/:Listing.id');
	}
	
	$index->addColumn('list_category_id','setCategoryName');
	$index->addColumn('user_id','setPosterName');
	$index->addColumn('created','showFormatedDate');
	
	function showFormatedDate($record)
	{
		return date('Y-m-d',strtotime($record['Listing']['created']));
	}	
	function setCategoryName($record)
	{
		return $record['ListCategory']['name'];
	}
	
	function setPosterName($record)
	{
		return $record['Listing']['poster']['display_name'];
	}
	
	function toggle_featured($model,$record)
	{
		if($record[$model]['is_featured'] == 1)
		{
			$action['title'] = __('Remove from feature');
			$action['url'] = '/admin/listings/change_feature_status/'.$record[$model]['id'];
		}else
		{
			$action['title'] = __('Make it featured');
			$action['url'] = '/admin/listings/change_feature_status/'.$record[$model]['id'];
		}
		return $action;
	}
	
	function publish_unpublish($model,$record)
	{
		if($record[$model]['status'] == Listing::PUBLISH)
		{
			$action['title'] = __('Unpublish');
			$action['url'] = '/admin/listings/changeStatus/'.$record[$model]['id'];
			$action['confirmMessage'] = __('Warning! Unpublising this listing will unpublish all associated reviews, comments and images. Are you sure you want to unpublish this listing ?');
		}else
		{
			$action['title'] = __('Publish');
			$action['url'] = '/admin/listings/changeStatus/'.$record[$model]['id'];
		}
		return $action;
	}
	if(cr("ListingConfig.business_owner"))
	{
		function setOwnerName($record)
		{
			$html = ClassRegistry::getObject('View')->loaded['html'];
			if($record['Listing']['owner_id'])
			{
				return $html->link($record['Listing']['ownerName'],'/admin/users/view/'.$record['Listing']['owner_id']);
			}
			return "N/A";
		}
	}
	$index->setPaginateSeparator(' ');
	echo $index->display($listingData);
}else
{
	echo "<h4>".__('No listings found!')."</h4>";
	
}
?>
<script type="text/javascript">
$(document).ready(function()
{
	//$('#ListingStatus').val('');
});
</script>